<template>
  <el-card style="margin: 5px 0px">
    <el-form class="elForm">
      <el-form-item label="职位搜索">
        <el-input placeholder="请你输入搜索内容" v-model="roleName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selfSearch">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card>
    <el-button icon="Plus" type="primary" @click="addRole">添加职位</el-button>
    <el-table border style="margin: 10px 0px" :data="allRoles">
      <el-table-column
        label="#"
        width="50"
        type="index"
        align="center"
      ></el-table-column>
      <el-table-column
        width="100"
        show-overflow-tooltip
        label="ID"
        align="center"
        prop="id"
      ></el-table-column>
      <el-table-column
        width="250"
        show-overflow-tooltip
        label="职位名称"
        align="center"
        prop="roleName"
      ></el-table-column>
      <el-table-column
        width="250"
        show-overflow-tooltip
        label="创建时间"
        align="center"
        prop="createTime"
      ></el-table-column>
      <el-table-column
        width="250"
        show-overflow-tooltip
        label="更新时间"
        align="center"
        prop="updateTime"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="操作"
        align="center"
        width="560"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            icon="User"
            @click="assignRole(row)"
          >
            分配权限
          </el-button>
          <el-button
            type="success"
            size="small"
            icon="Edit"
            @click="editRole(row)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="small"
            icon="Delete"
            @click="deleteRole(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[2, 5, 7, 10]"
      background
      layout=" prev, pager, next, jumper,->,total,sizes"
      :total="total"
      @size-change="getRoles"
      @current-change="getRoles"
    />
  </el-card>
  <!-- 添加或修改的弹窗 -->
  <el-dialog v-model="dialogVisite" :title="titleDialog">
    <el-form>
      <el-form-item label="职位名称">
        <el-input
          placeholder="请输入职位名称"
          v-model="dialogRoleData.roleName"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="default" size="default" @click="dialogVisite = false">
        取消
      </el-button>
      <el-button type="primary" size="default" @click="DiaAddRole">
        确定
      </el-button>
    </template>
  </el-dialog>
  <!-- 分配权限的抽屉 -->
  <el-drawer v-model="drawer" :title="titleDrawer">
    <template #default>
      <div>
        <el-tree
          ref="tree"
          :data="roleMunuData"
          :props="defaultProps"
          show-checkbox
          node-key="id"
          default-expand-all
          :default-checked-keys="roleIdList"
        />
      </div>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="reset()">取消</el-button>
        <el-button type="primary" @click="confirmClick">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  reqAddOrUpdateRole,
  reqAllMenuList,
  reqAllRoleList,
  reqRemoveRole,
  reqSetPermisstion,
} from '@/api/acl/role/index'
import { ElMessage } from 'element-plus'
import type { MenuList, MunuData, Records, RoleData } from '@/api/acl/role/type'
import { SettingStore } from '@/stores/SettingStore'
//页面页数
let pageNo = ref<number>(1)
//页面数量
let pageSize = ref<number>(5)
//页面总数
let total = ref<number>(40)
//全部角色的列表
let allRoles = ref<Records>([])
//搜索的角色名称
let roleName = ref<string>('')
//tree组件实例
let tree = ref()
//搜索方法
let selfSearch = () => {
  getRoles()
}
//重置方法
let settingStore = SettingStore()
const reset = () => {
  settingStore.refresh = !settingStore.refresh
}
//获取全部的角色
const getRoles = async (page: number = 1) => {
  pageNo.value = page
  let req = await reqAllRoleList(page, pageSize.value, roleName.value)
  if (req.code === 200) {
    total.value = req.data.total
    allRoles.value = req.data.records
  } else {
    ElMessage({ type: 'error', message: req.message })
  }
}
//页面加载完成
onMounted(() => {
  getRoles()
})

//******dialog***********/
//添加角色
const addRole = () => {
  dialogVisite.value = true
  titleDialog.value = '添加职位'
  Object.assign(dialogRoleData, { roleName: '' })
}
//修改角色
const editRole = (row: RoleData) => {
  dialogVisite.value = true
  titleDialog.value = '修改职位'
  Object.assign(dialogRoleData, row)
}
//dialog的显示和隐藏
let dialogVisite = ref<boolean>(false)
//Dialog的标题
let titleDialog = ref<string>('添加职位')
//职位角色
let dialogRoleData = reactive<RoleData>({ roleName: '' })
//添加角色
const DiaAddRole = async () => {
  let req = await reqAddOrUpdateRole(dialogRoleData)
  if (req.code === 200) {
    ElMessage({ type: 'success', message: req.message })
    dialogVisite.value = false
    getRoles(pageNo.value)
  } else {
    ElMessage({ type: 'error', message: req.message })
  }
}
//******dialog***********/

//删除角色
const deleteRole = async (row: RoleData) => {
  let req = await reqRemoveRole(row.id as number)
  if (req.code === 200) {
    ElMessage({ type: 'success', message: req.message })
    getRoles(pageNo.value)
  } else {
    ElMessage({ type: 'error', message: req.message })
  }
}
//******drawer*****/
//分配权限的角色信息
let roleInfo = ref<RoleData>({ roleName: '' })
//分配权限的数据
let roleMunuData = ref<MenuList>([])
//是否分配权限的Id
let roleIdList = ref<number[]>([])
//drawer的显示和隐藏
let drawer = ref<boolean>(false)
//Drawer的标题
let titleDrawer = ref<string>('分配权限')

//确定的点击事件
const confirmClick = async () => {
  //整理数据
  const id = roleInfo.value.id as number
  const arr = tree.value.getCheckedKeys()
  const arr1 = tree.value.getHalfCheckedKeys()
  let permissionId = arr.concat(arr1)
  let res = await reqSetPermisstion(id, permissionId)
  if (res.code == 200) {
    drawer.value = false
    ElMessage({ type: 'success', message: res.message })
    reset()
  } else {
    ElMessage({ type: 'error', message: res.message })
    reset()
  }
}
//分配权限
const assignRole = async (row: RoleData) => {
  drawer.value = true
  Object.assign(roleInfo.value, row)
  let res = await reqAllMenuList(row.id as number)
  if (res.code === 200) {
    roleMunuData.value = res.data
    roleIdList.value = filerSelectarr(res.data, roleIdList.value)
  } else {
    ElMessage({ type: 'error', message: res.message })
  }
}
const filerSelectarr = (allData: MenuList, initArr: any) => {
  allData.forEach((item) => {
    if (item.select && item.level == 4) {
      initArr.push(item.id)
    }
    if (item.children && item.children.length > 0) {
      filerSelectarr(item.children, initArr)
    }
  })
  return initArr
}

const defaultProps = {
  children: 'children',
  label: 'name',
}

//******drawer*****/
</script>
<style scoped lang="scss">
.elForm {
  display: flex;
  justify-content: space-between;
  height: 30px;
}
</style>
